/*
* $Id: ChangePointWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import apiary.db.managers.ConvertData;
import java.util.Vector;

import apiary.Apiary;
import apiary.db.managers.DBManager;
import apiary.db.FieldsValueStore.FieldsValueRecord;
import apiary.db.PointStore.PointRecord;
import apiary.ui.BackScreen;
import apiary.common.FormHelper;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.PointForm;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.PointForm.PointValue;
import apiary.ui.journal.UserFieldForm.UserField;

/**
 * Wizard of changing point
 * @author lexus
 */
public class ChangePointWizard extends Wizard
{
    /**
     * Constructor
     */
    public ChangePointWizard() {
        super(null);
        Init();
    }
    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ChangePointWizard(BackScreen parentForm) {
        super(null);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        PointForm pointForm = (PointForm) nextForm;
        ChooseValue value = (ChooseValue) formData.get(new Integer(1));
        DBManager dBManager = DBManager.getInstance();
        PointRecord point = dBManager.getPoint(value.id);
        if(point == null)
            return;
        PointValue pointValue = pointForm.new PointValue();
        ConvertData.PointRecordToValue(point, pointValue);
        Vector fieldsValue = dBManager.getFieldsValue(point.idFieldValue);
        Vector userFields = new Vector();
        for(int i = 0; i < fieldsValue.size(); i++) {
            FieldsValueRecord fieldValue = (FieldsValueRecord) fieldsValue.elementAt(i);
            UserField userField = pointForm.new UserField();
            ConvertData.FieldsValueToUserField(fieldValue, userField);
            userFields.addElement(userField);
        }
        pointValue.userFields = userFields;
        pointForm.setPointValue(pointValue);
    }

    /**
     * Saves result of wizard
     */
    public void save() {
        ChooseValue choose = (ChooseValue) formData.get(new Integer(1));
        PointValue point = (PointValue) formData.get(new Integer(2));
        DBManager.getInstance().changePoint(choose.id, point);
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChoosePoint"), idChoose, this, true);
        FormHelper.fillPointList((ChooseForm) firstForm);
        Integer idPoint = new Integer(2);
        PointForm form = new PointForm(apiary.getLocaleString("ChangePoint"), idPoint, this, false);
        form.setBackScreen(firstForm);
        formsNext.put(idChoose, form);
    }
}
